package com.example.nio.config;

import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.nio.commons.ChatMessage;
import com.example.nio.commons.Message;
import com.example.nio.service.MessageDisposeService;
import com.example.nio.utils.AgentUserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @ClassName DirectReceiver 消息接收监听
 * @Description TODO
 * @Author XavierLee
 * @Date 2020/2/26 9:54
 * @Version 1.0
 **/

@Component
@Slf4j
@RabbitListener(queues = "${spring.rabbitmq.directqueue}")
public class DirectReceiver {

    @Autowired
    private MessageDisposeService messageDisposeService;

    @RabbitHandler
    public void process(String result) {
        log.info("DirectReceiver消费者收到消息: " + result);
        try {
            ChatMessage message = JSONUtil.toBean(result,ChatMessage.class);
            // 用来判断是否客服的最后一条消息
            AgentUserUtil.setLastMessage(message.getFromUser(),false);
            messageDisposeService.messageHandling(message);
        } catch (Exception e){
            log.info("服务端rabbitmq消息解析失败");
        }
    }
}
